package com.security.door.controller.accessManage;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.security.door.controller.BaseController;
import com.security.door.core.wrapper.WrapMapper;
import com.security.door.core.wrapper.Wrapper;
import com.security.door.model.domain.baseUserRole;
import com.security.door.model.domain.baseUsers;
import com.security.door.model.dto.baseUsersDto;
import com.security.door.service.IbaseUserRoleService;
import com.security.door.service.IbaseUsersService;

import com.security.door.utils.AuthJwt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@Api(value = "WEB - baseUserController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class baseUserController extends BaseController {

    @Autowired
    HttpServletResponse httpServletResponse;
    @Autowired
    HttpServletRequest httpServletRequest;
    @Autowired
    IbaseUsersService ibaseUsersService;
    @Autowired
    IbaseUserRoleService ibaseUserRoleService;

    @PostMapping(value = "/user/login")
    @ApiOperation(value = "用户登录", notes = "用户登录")
    public Wrapper LoginAction(@RequestBody baseUsers entity) {
        baseUsers client = ibaseUsersService.selectOne(entity);
        if (null != client) {
            String token = AuthJwt.sign(entity.getUsername(), entity.getPassword());
            /////////////更新token////////
            client.setToken(token);
            ibaseUsersService.update(client);
            Map<String, String> map = new HashMap<>();
            map.put("token", token);
            map.put("username", entity.getUsername());
            return WrapMapper.ok(map);
        } else
            return WrapMapper.error("用户不存在");
    }

    @GetMapping(value = "/user/info")
    @ApiOperation(value = "用户信息", notes = "用户信息")
    public Wrapper LoginAction(@PathParam("token") String token) {
        baseUsers client = ibaseUsersService.selectByToken(token);
        if (null == client)
            return null;
        Map<String, Object> map = new HashMap<>();
        map.put("avatar", client.getAvatar());
        map.put("introduction", client.getRemark());
        map.put("name", client.getRealname());
        var roles = ibaseUserRoleService.selectRoleByUserId(client.getGuid());
        var roleCodes = roles.stream().map(baseUserRole::getRolecode).collect(Collectors.toList()).toArray();
        map.put("roles", roleCodes);
        return WrapMapper.ok(map);
    }

    @PostMapping("/user/queryListWithPage")
    @ApiOperation(value = "分页查询所有用户信息", notes = "分页查询所有用户信息")
    public Wrapper<PageInfo<baseUsers>> queryListWithPage(@RequestBody baseUsersDto entityDto) {
        var list = ibaseUsersService.queryListWithPage(entityDto);
        return WrapMapper.ok(list);
    }

    @PostMapping(value = "/user/checkCode")
    @ApiOperation(value = "判断用户是否存在", notes = "判断用户是否存在")
    public boolean checkCode(@RequestBody baseUsers entity) {
        return ibaseUsersService.checkCode(entity);
    }

    @PostMapping(value = "/user/addBaseUser")
    @ApiOperation(value = "添加用户", notes = "添加用户")
    public Wrapper addBaseUser(@RequestBody baseUsers entity) {
        try {
            if (null != entity) {
                if (null == entity.getGuid() || "".equals(entity.getGuid())) {
                    entity.setGuid(UUID.randomUUID().toString());
                    entity.setCreater(super.getUserName());
                    entity.setCreatetime(new Date());
                    ibaseUsersService.save(entity);
                } else {
                    ibaseUsersService.update(entity);
                }
            }
            return WrapMapper.ok("操作成功");
        } catch (Exception ex) {
            return WrapMapper.error(ex.toString());
        }
    }

    @GetMapping(value = "/user/delBaseUser/{guid}")
    @ApiOperation(value = "删除用户", notes = "删除用户")
    public Wrapper delBaseUser(@PathVariable("guid") String guid) {
        ibaseUsersService.deleteByKey(guid);
        return WrapMapper.ok("操作成功");
    }
}
